﻿using System;
using AccelerateSensor.Console.Tools;

namespace AccelerateSensor.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            LogHelper.InitLogPath();
            LogHelper.AddLog("Instance is starting.");

            var socketServer = new SocketServer();
            socketServer.Start(44440);

            System.Console.WriteLine();
            System.Console.WriteLine("Press key 'q' to stop");
            while (System.Console.ReadKey().KeyChar.ToString().ToUpper() != "Q")
            {
                System.Console.WriteLine();
            }

            socketServer.Stop();

            LogHelper.AddLog("Instance is shutting down.");
            LogHelper.Stop();

            System.Console.WriteLine();
            System.Console.WriteLine("Press any key to exit...");
            System.Console.ReadKey();
            System.Console.WriteLine();

            //var port = 2000;
            //var host = "127.0.0.1";
            ////创建终结点（EndPoint）
            //var ip = IPAddress.Parse(host); //把ip地址字符串转换为IPAddress类型的实例
            //var ipe = new IPEndPoint(ip, port); //用指定的端口和ip初始化IPEndPoint类的新实例
            ////创建socket并开始监听
            //var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //s.Bind(ipe);
            //s.Listen(0); //开始监听
            //System.Console.WriteLine("等待客户端连接");
            ////接受到client连接，为此连接建立新的socket，并接受信息
            //Socket temp = s.Accept(); //为新建连接创建新的socket
            //System.Console.WriteLine("建立连接");
            //string recvStr = "";
            //byte[] recvBytes = new byte[1024];
            //int bytes;
            //bytes = temp.Receive(recvBytes, recvBytes.Length, 0); //从客户端接受信息
            //recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
            ////给client端返回信息
            //System.Console.WriteLine("server get message:{0}", recvStr); //把客户端传来的信息显示出来
            //string sendStr = "ok!Client send message successful!";
            //byte[] bs = Encoding.ASCII.GetBytes(sendStr);
            //temp.Send(bs, bs.Length, 0); //返回信息给客户端
            //temp.Close();
            //s.Close();
            //System.Console.ReadLine();
        }
    }
}
